﻿using System;
using System.Collections.Generic;
using System.IO.Ports;
using System.Linq;
using System.Text;
using System.Windows.Input;
using YmSoft.Base.Ports;
using YmSoft.Data;

namespace YmSoft.Old.Vm
{
    public class VmWeatherStationOldSetting : BaseNotifyPropertyChanged
    {
        CfWeatherStationOld data = null;
        public VmWeatherStationOldSetting()
        {

        }
        public CfWeatherStationOld Data
        {
            get
            {

                if (data == null)
                {
                    if (IsInDesignMode)
                    {
                        data = new CfWeatherStationOld();
                    }
                    else
                    {
                        var repository = new CfWeatherStationOldRepository();
                        data = repository.GetCfExtension();
                        //data = new CfWeatherStationOld();
                    }
                }
                return data;
            }
        }

        ICommand saveCommand = null;
        public ICommand SaveCommand
        {
            get
            {
                if (saveCommand == null)
                {
                    saveCommand = new RelayCommand(() =>
                    {

                        if (!IsInDesignMode)
                        {
                            var repository = new CfWeatherStationOldRepository();
                            repository.SaveCfExtension(data);
                            repository.SubmitChanges();
                        }
                    });
                }
                return saveCommand;
            }
        }

        ICommand writeCommand = null;
        public ICommand WriteCommand
        {
            get
            {
                if (writeCommand != null)
                {
                    return writeCommand;
                }

                writeCommand = new RelayCommand(() =>
                      {
                          //using (var sp = new SerialPort(data.Port))
                          using (var sp = new SerialPort("COM3"))
                          {
                              WeatherStationSetMsg request = new WeatherStationSetMsg();
                              request.等待超时时间 = data.等待超时时间;
                              request.自动广播间隔时间 = data.自动广播间隔时间;
                              request.雨雪确认时间 = data.雨雪确认时间;
                              request.风速确认时间 = data.风速确认时间;
                              request.风速上限值 = data.风速上限值;

                              // configure serial port
                              sp.BaudRate = 9600;
                              sp.DataBits = 8;
                              sp.Parity = Parity.None;
                              sp.StopBits = StopBits.One;
                              sp.Open();

                              var device = SerialPortDevice.Create(sp);
                              var response = device.Read<WeatherStationReturnSetMsg, WeatherStationSetMsg>(request);

                              sp.Close();
                              if (response == null || response.状态 == 0)
                              {
                                  TagString = "写设备失败";
                                  return;
                              }
                              TagString = "写设备成功";
                          }

                      });

                return writeCommand;
            }
        }

        #region "TagString"

        private string _TagString = default(string);

        public string TagString
        {
            get
            {
                return _TagString;
            }
            set
            {
                if (_TagString == value)
                {
                    return;
                }
                var oldValue = _TagString;
                _TagString = value;
                NotifyPropertyChanged("TagString");
            }
        }

        #endregion


        #region "Hour1"

        public byte Hour1
        {
            get
            {
                return OldUtility.GetHour(Data.等待超时时间);
            }
            set
            {
                Data.等待超时时间 = OldUtility.GetTimeShort(value, Minute1, Second1);
                NotifyPropertyChanged("Hour1");
            }
        }
        #endregion

        #region "Minute1"

        public byte Minute1
        {
            get
            {
                return OldUtility.GetMinute(Data.等待超时时间);
            }
            set
            {
                Data.等待超时时间 = OldUtility.GetTimeShort(Hour1, value, Second1);
                NotifyPropertyChanged("Minute1");
            }
        }



        #endregion

        #region "Second1"

        public byte Second1
        {
            get
            {
                return OldUtility.GetSecond(Data.等待超时时间);
            }
            set
            {
                Data.等待超时时间 = OldUtility.GetTimeShort(Hour1, Minute1, value);
                NotifyPropertyChanged("Second1");
            }
        }

        #endregion

        #region "Hour2"

        public byte Hour2
        {
            get
            {
                return OldUtility.GetHour(Data.自动广播间隔时间);
            }
            set
            {
                Data.自动广播间隔时间 = OldUtility.GetTimeShort(value, Minute2, Second2);
                NotifyPropertyChanged("Hour2");
            }
        }
        #endregion

        #region "Minute2"

        public byte Minute2
        {
            get
            {
                return OldUtility.GetMinute(Data.自动广播间隔时间);
            }
            set
            {
                Data.自动广播间隔时间 = OldUtility.GetTimeShort(Hour2, value, Second2);
                NotifyPropertyChanged("Minute2");
            }
        }



        #endregion

        #region "Second2"

        public byte Second2
        {
            get
            {
                return OldUtility.GetSecond(Data.自动广播间隔时间);
            }
            set
            {
                Data.自动广播间隔时间 = OldUtility.GetTimeShort(Hour2, Minute2, value);
                NotifyPropertyChanged("Second2");
            }
        }

        #endregion

        #region "Hour3"

        public byte Hour3
        {
            get
            {
                return OldUtility.GetHour(Data.雨雪确认时间);
            }
            set
            {
                Data.雨雪确认时间 = OldUtility.GetTimeShort(value, Minute3, Second3);
                NotifyPropertyChanged("Hour3");
            }
        }
        #endregion

        #region "Minute3"

        public byte Minute3
        {
            get
            {
                return OldUtility.GetMinute(Data.雨雪确认时间);
            }
            set
            {
                Data.雨雪确认时间 = OldUtility.GetTimeShort(Hour3, value, Second3);
                NotifyPropertyChanged("Minute3");
            }
        }



        #endregion

        #region "Second3"

        public byte Second3
        {
            get
            {
                return OldUtility.GetSecond(Data.雨雪确认时间);
            }
            set
            {
                Data.雨雪确认时间 = OldUtility.GetTimeShort(Hour3, Minute3, value);
                NotifyPropertyChanged("Second3");
            }
        }

        #endregion

        #region "Hour4"

        public byte Hour4
        {
            get
            {
                return OldUtility.GetHour(Data.风速确认时间);
            }
            set
            {
                Data.风速确认时间 = OldUtility.GetTimeShort(value, Minute4, Second4);
                NotifyPropertyChanged("Hour4");
            }
        }
        #endregion

        #region "Minute4"

        public byte Minute4
        {
            get
            {
                return OldUtility.GetMinute(Data.风速确认时间);
            }
            set
            {
                Data.风速确认时间 = OldUtility.GetTimeShort(Hour4, value, Second4);
                NotifyPropertyChanged("Minute4");
            }
        }



        #endregion

        #region "Second4"

        public byte Second4
        {
            get
            {
                return OldUtility.GetSecond(Data.风速确认时间);
            }
            set
            {
                Data.风速确认时间 = OldUtility.GetTimeShort(Hour4, Minute4, value);
                NotifyPropertyChanged("Second4");
            }
        }

        #endregion

    }
}
